package com.jsyh.maidu.adapter.home;


import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.me.StoreActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.Stores;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/11.
 */

public class GridHolder extends MyBaseViewHolder {


    private MyGridView gridView;
    private ImageView lineView;
    private DisplayMetrics dm;
    private int NUM = 3; // 每行显示个数

    public GridHolder(View itemView, String src, final List<Stores> data, final Context context) {
        super(itemView);
        gridView = (MyGridView) itemView.findViewById(R.id.collect_grid);
        lineView = (ImageView) itemView.findViewById(R.id.collect_line_img);
        Picasso.with(context).load(ConfigValue.IMG_IP + src).into(lineView);
        ComprehensiveAdapter adapter = new ComprehensiveAdapter(context, data);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(context, StoreActivity.class);
                intent.putExtra("suppid",data.get(i).getSupplier_id());
                context.startActivity(intent);
            }
        });

        getScreenDen(context);
        setValue(adapter);

    }

    private void setValue(ComprehensiveAdapter adapter) {
        int count = adapter.getCount();
        int columns = (count % 2 == 0) ? count / 2 : count / 2 + 1;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columns * dm.widthPixels / NUM,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        gridView.setLayoutParams(params);
        gridView.setColumnWidth(dm.widthPixels / NUM);
        gridView.setStretchMode(GridView.NO_STRETCH);
        if (count <= 3) {
            gridView.setNumColumns(count);
        } else {
            gridView.setNumColumns(columns);
        }
    }
    private void getScreenDen(Context context) {
        dm = new DisplayMetrics();
        ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
    }

    @Override
    public void bindData() {

    }
}
